import model.P3DMObjectFile;

/*
 * TextMain.java Created on Jan 6, 2008 by M.C.Boulahiya
 */

/**
 * @author M.C.Boulahiya
 *
 * Loads a set of files (OBJ) with their associated curvatures (CUR) and calculates 
 * their signature, related to Moving Frames, Joint Invariants and Joint Differential
 * Invariants. These signatures are stored in files (SIG#).
 * 
 * STEPS :
 *  1- Loading the geometry contained in an OBJ file and the associated curvatures 
 *  		file (creating the CUR file if it don't exist)  ==> DONE
 *  2- Extracting NB feature points ==> DONE
 *  3- Computing the signature for each 3 points ==> DONE
 *  4- Saving the signature file ==> DONE
 *  5- Writing a script file to compute the signature for each OBJ file (TODO)
 *  6- Extraction des bande de profils ==> DONE
 *  
 *  TODO SIMPLIFICATIONS : Extraire les opérations de calcul des courbures et des 
 *  signatures dans d'autres classes !
 *  
 *  exempla d'appel java -Xms128m -Xmx448m TextMain 50 ../../DataBase/Potteries/ObjFiles/FScanD27-Bord.obj
 */
public class TextMain {
	private static P3DMObjectFile file;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// ../../DataBase/Potteries/ObjFiles/ScanD27b-010ko.obj
		// FScanD27.obj
		System.out.println(args[0] + " feature points on file: " + args[1]);

		loadObjFile(args[1]);
		
		int NB_FEATURE_POINTS = new Integer(args[0]);
		creatingSignatures(NB_FEATURE_POINTS);
	}

	/**
	 * Read an OBJ file and populate the coordArray, normArray and curvaArray arrays.
	 * 
	 * @param filename
	 */
	private static void loadObjFile(String filename) {
		int flags = P3DMObjectFile.RECENTER; // RECENTER is used for "Convert ArrayLists to arrays"
		float creaseAngle = (float) (60.0 * Math.PI / 180.0);
		
		file = new P3DMObjectFile(flags, creaseAngle, true /* quick */);
		if (filename.endsWith("FScanD27.obj") || filename.endsWith("ScanRitt5.obj")
				|| (filename.endsWith("ScanD27b-760ko.obj"))) {		
			file.setProfileExtraction(true);
			System.out.println("Profile Extraction On");
		}
		long time = System.currentTimeMillis();
		try {
			file.textLoad(filename);
		}
		catch (Exception exception) {
			//FileNotFoundException, ParsingErrorException, IncorrectFormatException
			System.err.println(exception);
			System.exit(1);
		}
		time = System.currentTimeMillis() - time;
		System.out.println("load entier: " + time + " ms");
	} // End of loadObjFile(String)
	
	/**
	 * Selects feature points and computes the signature 3 by 3. 
	 * Saving the signatures into a SIG# file.
	 */
	private static void creatingSignatures(int NB_FEATURE_POINTS) {
		long time = System.currentTimeMillis();
		file.computingSignatures(NB_FEATURE_POINTS);
		time = System.currentTimeMillis() - time;
		System.out.println("Signature creation: " + time + " ms");
	} // End of creatingSignatures

} // End of class TextMain

// End of file TextMain.java